package com.copa.gameStore.controller;

import com.copa.gameStore.application.GameGoodsApplicationService;
import com.copa.gameStore.domain.GameGoods;
import com.copa.gameStore.domain.Role;
import com.copa.gameStore.dto.GameGoodsDto;
import com.copa.gameStore.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-09-26 18:19
 * @function
 */
@RestController
@Api(tags = "游戏商品服务")
public class GameGoodsController {

    @Resource
    private GameGoodsApplicationService gameGoodsApplicationService;

    @RolesAllowed(Role.ADMIN)
    @ApiOperation("添加商品")
    @PostMapping("add")
    public ResponseVo<String> post(@RequestBody @Validated GameGoodsDto gameGoodsDto) {
        return gameGoodsApplicationService.add(gameGoodsDto);
    }

    @ApiOperation("根据游戏名获取游戏商品信息（管理员）")
    @GetMapping("admin")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<List<GameGoods>> getAdmin(@RequestParam(value = "name", defaultValue = "") String name) {
        return gameGoodsApplicationService.getAdmin(name);
    }

    @ApiOperation("游戏详情（管理员）")
    @GetMapping("admin/{id}")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<GameGoods> getDetailAdmin(@PathVariable("id") Long id) {
        return gameGoodsApplicationService.getDetailAdmin(id);
    }

    @RolesAllowed(Role.ADMIN)
    @ApiOperation("修改商品信息")
    @PutMapping("update")
    public ResponseVo<String> put(@RequestBody @Validated GameGoodsDto gameGoodsDto) {
        return gameGoodsApplicationService.modify(gameGoodsDto);
    }

    @ApiOperation("根据游戏名获取游戏商品信息（用户）")
    @GetMapping("get")
    public ResponseVo<List<GameGoods>> get(@RequestParam(value = "name", defaultValue = "") String name) {
        return gameGoodsApplicationService.get(name);
    }

    @ApiOperation("游戏详情（用户）")
    @GetMapping("{id}")
    public ResponseVo<GameGoods> getDetail(@PathVariable("id") Long id) {
        return gameGoodsApplicationService.getDetail(id);
    }

    @ApiOperation("此方法为feign调用，校验商品")
    @GetMapping("validateGoods/{gameGoodsId}")
    public Boolean validateGoods(@PathVariable("gameGoodsId") Long gameGoodsId) {
        return gameGoodsApplicationService.validateGoods(gameGoodsId);
    }

    @ApiOperation("此方法为feign调用，查询商品id")
    @GetMapping("getById/{gameGoodsId}")
    public GameGoods getById(@PathVariable("gameGoodsId") Long gameGoodsId) {
        return gameGoodsApplicationService.getById(gameGoodsId);
    }

    @ApiOperation("此方法为feign调用，库存操作")
    @GetMapping("stock")
    public void stock(@RequestParam("gameGoodsId") Long gameGoodsId,
                      @RequestParam("num") Integer num) {
        gameGoodsApplicationService.stock(gameGoodsId, num);
    }
}
